package org.yxing.palpalgps.adapter;

import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.models.SimpleUser;
import org.yxing.palpalgps.utils.Helper;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class SelectFriendAdapter extends ArrayAdapter<SimpleUser> {
    
    private AsyncImageLoader mAsyncImageLoader;
    
    static class ViewHolder {
        protected TextView name;
        protected ImageView portrait;
        protected CheckBox check;
    }
    
    public SelectFriendAdapter(Context context) {
        super(context, R.layout.select_friend_item);
        mAsyncImageLoader = new AsyncImageLoader();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final SimpleUser simpleUser = getItem(position);
        if (convertView == null) {
            Log.v("TAG", "getView");
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.select_friend_item, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.name = (TextView) convertView.findViewById(R.id.textViewName);
            viewHolder.portrait = (ImageView) convertView.findViewById(R.id.imageViewPortrait);
            viewHolder.check = (CheckBox) convertView.findViewById(R.id.check);
            convertView.setTag(viewHolder);
        }
        
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        
        viewHolder.check.setChecked(simpleUser.isSelected());
        viewHolder.name.setText(simpleUser.getName());
        mAsyncImageLoader.loadDrawable(Helper.getSmallPictureUrl(simpleUser.getPortrait()), viewHolder.portrait);
        
        return convertView;
    }

}
